package cn.kgc.shiro03.shiro;

import cn.kgc.shiro03.entity.Role;
import cn.kgc.shiro03.entity.User;
import cn.kgc.shiro03.entity.UserRole;
import cn.kgc.shiro03.mapper.RoleMapper;
import cn.kgc.shiro03.mapper.UserMapper;
import cn.kgc.shiro03.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/7/28
 * @Description:
 */
public class CustomerRealm extends AuthorizingRealm {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;
    // 授权方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("=================doGetAuthorizationInfo===============");
        // 根据用户身份信息  查询用户角色信息
        String primaryPrincipal = (String) principals.getPrimaryPrincipal();
        // 根据用户名 查询用户的id
        LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
        lambda.eq(User::getUsername,primaryPrincipal);
        User user = userMapper.selectOne(lambda);
        Integer userId = user.getId();
        // 根据用户id  查询用户角色关联表  user_role  rid
        LambdaQueryWrapper<UserRole> lambda1 = new QueryWrapper<UserRole>().lambda();
        lambda1.eq(UserRole::getUid,userId);
        List<UserRole> userRoles = userRoleMapper.selectList(lambda1);

        //ArrayList<Integer> rIds = new ArrayList<>();
        //for (UserRole userRole : userRoles) {
        //    Integer rid = userRole.getRid();
        //    rIds.add(rid);
        //}

        List<Integer> rids = userRoles.stream().map(ur -> ur.getRid()).collect(Collectors.toList());

        // 根据角色id  查询角色表   role_en
        List<Role> roles = roleMapper.selectBatchIds(rids);
        List<String> roleEns = roles.stream().map(role -> role.getRoleEn()).collect(Collectors.toList());

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(roleEns);
        return simpleAuthorizationInfo;
    }

    // 认证方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
        System.out.println("==========userMapper============ " + userMapper);
        // TODO 查询数据库  根据用户名 查询用户信息

        LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
        lambda.eq(User::getUsername,username);

        User user = userMapper.selectOne(lambda);  // user
        if (ObjectUtils.isNotNull(user)){
            return  new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(), ByteSource.Util.bytes(user.getSalt()),this.getName());
        }
        return null;
    }
}
